package com.peycash.reverso.endpoint;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.jpos.iso.ISOChannel;
import org.jpos.iso.ISODate;
import org.jpos.iso.ISOException;
import org.jpos.iso.ISOMsg;
import org.jpos.iso.channel.ASCIIChannel;
import org.jpos.iso.packager.ISO87APackager;

import com.peycash.reverso.dto.R001;
import com.peycash.reverso.dto.T001;

public class PeycashISOUtilsImpl implements PeyCashISOUtilsService {

        @Override
        public ISOMsg translateReverso(T001 t001) throws ISOException {
                ISOMsg request = new ISOMsg();
                request.setMTI("0420");
                /* field 4 is not present */
                request.set(3, "00809");
                request.set(4, "0");
                String yyyymmdd = t001.getF6();
                String hhmmss = t001.getF7();
                Date requestDate = parseDate(yyyymmdd, hhmmss);
                request.set(7, ISODate.getDateTime(requestDate));
                String audit = t001.getF4();
                request.set(11, audit);
                request.set(12, ISODate.getTime(requestDate));
                request.set(13, ISODate.getDate(requestDate));
                request.set(22, "000");
                String idPeycashTx = t001.getF5();
                String referenceNumber = idPeycashTx.substring(0, 12);
                request.set(37, referenceNumber);
                request.set(38,"1");
                request.set(39, "68");
                String terminal = t001.getF3();
                request.set(41, terminal);
                String partnerShopAff = t001.getF9();
                request.set(42, partnerShopAff );
                String partnerAff = t001.getF8();
                request.set(43, partnerAff);
                request.set(102, idPeycashTx);
                return request;
        }

        @Override
        public ISOMsg sendMessage(ISOMsg m) throws IOException, ISOException {
                // String host = "192.168.1.120";//"192.168.1.39";
                String host = "localhost";
                ISOChannel channel = new ASCIIChannel(host, 10010,
                                new ISO87APackager());
                channel.connect();
                channel.send(m);
                ISOMsg r = channel.receive();
                channel.disconnect();
                return r;
        }

        @Override
        public R001 translateMessage(ISOMsg r) {
                R001 r001 = new R001();
                String messageResp = null;
                String responseCode = r.getString(39);
                messageResp = MessageRespUtils.getMessageResp(responseCode);
                r001.setR1(responseCode);
                r001.setR2(messageResp);
                if (responseCode.equals("00")) {
                        String field11 = r.getString(11);
                        String field37 = r.getString(37);
                        String response3 = field11 + field37;
                        response3 = StringUtils.leftPad(response3, 20, "0");
                        r001.setR3(response3);
                        String autCode = r.getString(38);
                        autCode = StringUtils.leftPad(autCode, 10, "0");
                        r001.setR4(autCode);
                        String ticket = r.getString(127);
                        r001.setR5(ticket);
                        String dateFormat = r.getString(7);
                        Date parsed = parseDate(dateFormat);
                        String ddmmyyyy = getddMMyyyy(parsed);
                        r001.setR6(ddmmyyyy);
                        String hhmmss = gethhmmss(parsed);
                        r001.setR7(hhmmss);
                }
                return r001;
        }

        private String getddMMyyyy(Date d) {
                DateTime time = new DateTime(d);
                int dayOfMonth = time.getDayOfMonth();
                int month_ = time.getMonthOfYear();
                int year_ = time.getYear();
                String day = StringUtils.leftPad(String.valueOf(dayOfMonth), 2,
                                "0");
                String month = StringUtils.leftPad(String.valueOf(month_), 2,
                                "0");
                String year = String.valueOf(year_);
                String returnValue = day + month + year;
                return returnValue;
        }

        private String gethhmmss(Date d) {
                DateTime time = new DateTime(d);
                int _hour = time.getHourOfDay();
                int _min = time.getMinuteOfHour();
                int _sec = time.getSecondOfMinute();
                String hour = StringUtils
                                .leftPad(String.valueOf(_hour), 2, "0");
                String min = StringUtils.leftPad(String.valueOf(_min), 2, "0");
                String sec = StringUtils.leftPad(String.valueOf(_sec), 2, "0");
                String returnValue = hour + min + sec;
                return returnValue;
        }

        private Date parseDate(String mmddhhmmss) {
                DateTime time1 = DateTime.now();
                int year = time1.getYear();
                String format = String.valueOf(year) + mmddhhmmss;
                DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyyMMddHHmmss");
                DateTime time = fmt.parseDateTime(format);
                return time.toDate();
        }

        private Date parseDate(String yyyymmdd, String hhmmss) {
                DateTimeFormatter fmt = DateTimeFormat
                                .forPattern("yyyyMMddHHmmss");
                DateTime time = fmt.parseDateTime(yyyymmdd + hhmmss);
                return time.toDate();
        }

        private void logMessage(ISOMsg m) throws IOException, ISOException {
                ByteArrayOutputStream os = new ByteArrayOutputStream();
                m.pack(os);
                String message = os.toString();
                System.out.println(message);
                os.close();
        }

}
